﻿namespace MelodysDemo.Application.Clazzs
{
    public class ClazzService : IClazzService
    {
        readonly IRepository<SurveyClazz, int> _surveyClazz;//班级
        public ClazzService(IRepository<SurveyClazz, int> surveyClazz)
        {
            _surveyClazz = surveyClazz;
        }
        public async Task<PagedResultDto<SurveyClazz>> GetResultDto(ClazzInput input)
        {
            var q = _surveyClazz.GetAll().Where(c => c.IsDelete == 0);

            //班级名
            if (input.ClazzName is { Length: > 0 })
            {
                q = q.Where(c => c.ClazzName.Contains(input.ClazzName));
            }
            //班主任
            if (input.AdviserName is { Length: > 0 })
            {
                q = q.Where(c => c.AdviserName.Contains(input.AdviserName));
            }
            //授课老师
            if (input.TeacherName is { Length: > 0 })
            {
                q = q.Where(c => c.TeacherName.Contains(input.TeacherName));
            }
            //状态
            if (input.Status > 0)
            {
                q = q.Where(c => c.Status == input.Status);
            }

            var count = q.Count();
            var list = await q.OrderByDescending(c => c.Id)
                                               .Skip((input.Page - 1) * input.Limit).Take(input.Limit)
                                               .AsNoTracking().ToListAsync();
            return new PagedResultDto<SurveyClazz>
            {
                Page = input.Page,
                Limit = input.Limit,
                TotalCount = count,
                Data = list
            };
        }
    }
}
